<?php

/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2025/6/23
 * Time: 14:40
 */

namespace App\Admin\Services;

use App\Models\WorkOrder;
use App\Models\WorkOrderComponent;
use App\Models\WorkOrderDispatch;

class WorkOrderService
{
    public static function detail($id)
    {
        return WorkOrder::find($id);
    }

    public static function orderCreate($param = [], $view_type = 1)
    {
        $opening_at = new \DateTime($param['opening_at']);

        $order_id = WorkOrder::create([
            'user_id' => $param['user_id'],
            'user_email' => $param['user_email'],
            'user_name' => $param['user_name'],
            'user_phone' => $param['user_phone'],
            'is_appointment' => $param['is_appointment'],
            'is_pre_spray' => $param['is_pre_spray'],
            'is_self_pay' => $param['is_self_pay'],
            'remarks' => $param['remarks'],
            'factory_year' => $param['factory_year'],
            'opening_at' => $opening_at,
            'pre_delivery_at' => $param['pre_delivery_at'],
            'service_advisor_id' => $param['service_advisor_id'],
            'is_accident' => $param['is_accident'] ?? 0,
            'is_return' => $param['is_return'] ?? 0,
            'is_urgent' => $param['is_urgent'],
            'brand_id' => $param['brand_id'],
            'workshop_id' => $param['workshop_id'] ?? 0,
            'car_id' => $param['car_id'],
            'car_number' => $param['car_number'],
            'car_frame' => $param['car_frame'],
            'order_sn' => $param['order_sn'],
            'maintenance_days' => $param['maintenance_days'],
            'status' => ORDER_WAIT_MAINTENANCE
        ])->id;

        if ($param['paint_parts']) {
            foreach ($param['paint_parts'] as $key => $value) {
                WorkOrderComponent::create([
                    'work_order_id' => $order_id,
                    'type' => COMPONENT_SPRAY_PAINT,
                    'component_maintenance_id' => $value['id'],
                    'title' => $value['title'],
                    'position_type' => $value['position_type'],
                    'damage_level_type' => damageLevelType($value['damage_level_type']),
                    'standard_quantity_base' => $value['standard_quantity_base'],
                    'standard_working_base' => $value['standard_working_base'],
                    'performance_quantity_base' => $value['performance_quantity_base'],
                    'performance_working_base' => $value['performance_working_base'],
                    'damage_coefficient' => $value['damage_new_coefficient']
                ]);
            }
        }

        if ($param['sheet_metal_parts']) {
            foreach ($param['sheet_metal_parts'] as $key => $value) {
                WorkOrderComponent::create([
                    'work_order_id' => $order_id,
                    'type' => COMPONENT_SHEET_METAL,
                    'component_maintenance_id' => $value['id'],
                    'title' => $value['title'],
                    'position_type' => $value['position_type'],
                    'damage_level_type' => damageLevelType($value['damage_level_type']),
                    'standard_quantity_base' => $value['standard_quantity_base'],
                    'standard_working_base' => $value['standard_working_base'],
                    'performance_quantity_base' => $value['performance_quantity_base'],
                    'performance_working_base' => $value['performance_working_base'],
                    'damage_coefficient' => $value['damage_new_coefficient']
                ]);
            }
        }

        WorkOrderDispatch::create([
            'work_order_id' => $order_id,
            'processes_archives_id' => 1,
            'processes_archives_title' => '待维修',
            'user_id' => $param['user_id'],
            'user_name' => $param['user_name'],
            'repair_status' => PROCESSES_IN,
            'actual_start_at' => date('Y-m-d H:i:s')
        ]);

        WorkOrderOperationLog::save([
            'work_order_id' => $order_id,
            'content' => '新增工单',
            'type' => $view_type,
            'operation_id' => $param['user_id']
        ]);
    }

    public static function orderUpdate($param = [], $view_type = 1)
    {
        $opening_at = new \DateTime($param['opening_at']);

        WorkOrder::where('id', $param['id'])->update([
            'user_id' => $param['user_id'],
            'user_email' => $param['user_email'] ?? '',
            'user_name' => $param['user_name'],
            'user_phone' => $param['user_phone'],
            'is_appointment' => $param['is_appointment'],
            'is_pre_spray' => $param['is_pre_spray'],
            'is_self_pay' => $param['is_self_pay'],
            'remarks' => $param['remarks'] ?? '',
            'factory_year' => $param['factory_year'],
            'opening_at' => $opening_at,
            'pre_delivery_at' => $param['pre_delivery_at'],
            'service_advisor_id' => $param['service_advisor_id'],
            'is_accident' => $param['is_accident'] ?? 0,
            'is_return' => $param['is_return'] ?? 0,
            'is_urgent' => $param['is_urgent'],
            'brand_id' => $param['brand_id'],
            'workshop_id' => $param['workshop_id'] ?? 0,
            'car_id' => $param['car_id'],
            'car_number' => $param['car_number'],
            'car_frame' => $param['car_frame'],
//            'order_sn' => $param['order_sn'],
            'maintenance_days' => $param['maintenance_days'] ?? '',
        ]);

        if ($param['paint_parts']) {
            $paint_parts_ids = WorkOrderComponent::where('work_order_id', $param['id'])
                ->where('type', COMPONENT_SPRAY_PAINT)
                ->pluck('component_maintenance_id')
                ->toArray();

            $secondArrayIds = array_column($param['paint_parts'], 'id');

            $filteredArray = array_filter($paint_parts_ids, function($item) use ($secondArrayIds) {
                return !in_array($item, $secondArrayIds, true); // 严格模式检查
            });

            if ($filteredArray) {
                foreach ($filteredArray as $value) {
                    WorkOrderComponent::where('work_order_id', $param['id'])
                        ->where('component_maintenance_id', $value)
                        ->delete();
                }
            }

            foreach ($param['paint_parts'] as $key => $value) {
                $component_maintenance_id = isset($value['component_maintenance_id']) ? $value['component_maintenance_id'] : $value['id'];

                if (in_array($component_maintenance_id, $paint_parts_ids)) {
                    WorkOrderComponent::where('work_order_id', $param['id'])
                        ->where('component_maintenance_id', $component_maintenance_id)
                        ->update([
                            'position_type' => $value['position_type'],
                            'damage_level_type' => damageLevelType($value['damage_level_type']),
                            'standard_quantity_base' => $value['standard_quantity_base'],
                            'standard_working_base' => $value['standard_working_base'],
                            'performance_quantity_base' => $value['performance_quantity_base'],
                            'performance_working_base' => $value['performance_working_base'],
                            'damage_coefficient' => isset($value['damage_new_coefficient']) ? $value['damage_new_coefficient'] : $value['damage_coefficient']
                        ]);
                } else {
                    WorkOrderComponent::create([
                        'work_order_id' => $param['id'],
                        'type' => COMPONENT_SPRAY_PAINT,
                        'component_maintenance_id' => $value['id'],
                        'title' => $value['title'],
                        'position_type' => $value['position_type'],
                        'damage_level_type' => damageLevelType($value['damage_level_type']),
                        'standard_quantity_base' => $value['standard_quantity_base'],
                        'standard_working_base' => $value['standard_working_base'],
                        'performance_quantity_base' => $value['performance_quantity_base'],
                        'performance_working_base' => $value['performance_working_base'],
                        'damage_coefficient' => isset($value['damage_new_coefficient']) ? $value['damage_new_coefficient'] : $value['damage_coefficient']
                    ]);
                }
            }
        }

        if ($param['sheet_metal_parts']) {
            $sheet_metal_parts_ids = WorkOrderComponent::where('work_order_id', $param['id'])
                ->where('type', COMPONENT_SHEET_METAL)
                ->pluck('component_maintenance_id')
                ->toArray();

            $secondArrayIds = array_column($param['sheet_metal_parts'], 'id');

            $filteredArray = array_filter($sheet_metal_parts_ids, function($item) use ($secondArrayIds) {
                return !in_array($item, $secondArrayIds, true); // 严格模式检查
            });

            if ($filteredArray) {
                foreach ($filteredArray as $value) {
                    WorkOrderComponent::where('work_order_id', $param['id'])
                        ->where('component_maintenance_id', $value)
                        ->delete();
                }
            }

            foreach ($param['sheet_metal_parts'] as $key => $value) {
                $component_maintenance_id = isset($value['component_maintenance_id']) ? $value['component_maintenance_id'] : $value['id'];

                if (in_array($component_maintenance_id, $sheet_metal_parts_ids)) {
                    WorkOrderComponent::where('work_order_id', $param['id'])
                        ->where('component_maintenance_id', $component_maintenance_id)
                        ->update([
                            'position_type' => $value['position_type'],
                            'damage_level_type' => damageLevelType($value['damage_level_type']),
                            'standard_quantity_base' => $value['standard_quantity_base'],
                            'standard_working_base' => $value['standard_working_base'],
                            'performance_quantity_base' => $value['performance_quantity_base'],
                            'performance_working_base' => $value['performance_working_base'],
                            'damage_coefficient' => isset($value['damage_new_coefficient']) ? $value['damage_new_coefficient'] : $value['damage_coefficient']
                        ]);
                } else {
                    WorkOrderComponent::create([
                        'work_order_id' => $param['id'],
                        'type' => COMPONENT_SHEET_METAL,
                        'component_maintenance_id' => $value['id'],
                        'title' => $value['title'],
                        'position_type' => $value['position_type'],
                        'damage_level_type' => damageLevelType($value['damage_level_type']),
                        'standard_quantity_base' => $value['standard_quantity_base'],
                        'standard_working_base' => $value['standard_working_base'],
                        'performance_quantity_base' => $value['performance_quantity_base'],
                        'performance_working_base' => $value['performance_working_base'],
                        'damage_coefficient' => isset($value['damage_new_coefficient']) ? $value['damage_new_coefficient'] : $value['damage_coefficient']
                    ]);
                }
            }
        }

        WorkOrderOperationLog::save([
            'work_order_id' => $param['id'],
            'content' => '修改工单',
            'type' => $view_type,
            'operation_id' => $param['user_id']
        ]);
    }
}